<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * 处理各个cdr
 * Class CdrCommand
 * @package Icsoc\Wincdr\Command
 */
class CdrCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('command:cdr')
            ->addOption('name', 'c', InputOption::VALUE_REQUIRED, '消费者名称')
            ->setDescription('consumer %s from rabbitmq to the elasticsearch');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $consumerName = $input->getOption('name');
        try {
            $className = "\\App\\Command\\".$consumerName.'Consumer';
            $class = new \ReflectionClass($className);
            $obj = $class->newInstance();
            $obj->execute();
        } catch (\ReflectionException $e) {
            $output->writeln(sprintf("消费者类不存在【%s】,message【%s】", $consumerName, $e->getMessage()));
        } catch (\Exception $e) {
            $output->writeln(sprintf("消费者【%s】,运行异常【%s】", $consumerName, $e->getTraceAsString()));
        }
    }
}